package com.shopping.servlet;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.shopping.service.UserService;
import com.shopping.service.impl.UserServiceImpl;

/**
 * @author zjh
 *
 */
@WebServlet("/login.do")
public class LoginServlet extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		UserService us= new UserServiceImpl();
		Map<String,Object> map = us.userLogin(username, password);
		if(map.get("loginUser") !=null) {
			request.getSession().setAttribute("loginUser", map.get("loginUser"));
			request.getRequestDispatcher("index.jsp").forward(request, response);
		}else {
			request.setAttribute("errorCode", map.get("errorCode"));
			request.getRequestDispatcher("login.jsp").forward(request, response);
		}
	}
	

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
